﻿// Helper class to decouple animations (repaints) from inputs that affect it
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

    var Debouncer = Wjs.defineOnce({

        type: 'Debouncer',

        internal: {

            construct: function (cfg) {
                cfg = cfg || {};

                this.callback = cfg.callback;
                this.data = cfg.data;
                this.tickRequested = false;
            }

        },

        nextTick: function () {
            var me = this;

            if (!me.tickRequested) {

                requestAnimationFrame(function () {
                    me.tickRequested = false;

                    me.callback();
                });
            }

            me.tickRequested = true; // Do not request more animations until it is cleared
        }

    });

    return Debouncer;
});